#include "server.h"

void * server_side ()           // on lance un serveur
{       
        registerrpc(/* prognum */ PROGNUM,
                        /* versnum */ VERSNUM,
                        /* procnum */ PROCNUM,
                        /* pointer on function */ serveur,
                        /* argument decoding */ (xdrproc_t)xdr_message,
                        /* function result encoding */ (xdrproc_t)xdr_int);

        svc_run();              /* server ready for clients */
        
        pthread_exit(0);
}


int * serveur (Message * msg)
{
        static int ret;                         // returned at the end of the 'serveur'

        int i;
        int p1,p2;      
        pthread_t d1,d2; 
        //TODO a remettre
        //p1 = pthread_create (&d1, NULL, server_side, NULL);   // thread pour lancer un autre serveur...
                if(msg->type==CONNECTION)               // Message d'arrivée sur le "serveur" 
                {
                        printf("* %s s'est connecté(e).\n",msg->nameSender);
                
                        connectionContact(msg->nameSender);
                        ret = METOO;
                }
                else
                if(msg->type==DECONNECTION)     // Message de sortie du "serveur"
                {
                        printf("* %s s'est déconnecté(e).\n",msg->nameSender);
                
                        deconnectionContact(msg->nameSender);

                        ret = STANDARD; 
                }
                else
                if(msg->type==BACKSEARCH)               // Message réponse à un envoi vers inconnu pour établir le pont
                {       
                        printf("* %s a été trouvé et ajouté aux contacts.\n",msg->nameSender);
                        
                        addContact(msg->nameSender, msg->ipSender);
                        
                        ret = STANDARD;
                }
                else
                if(msg->type==STANDARD)         // Message standard
                {               
                        printf(" ...\n# %s@%s : %s\n> ... ", msg->nameSender,msg->nameReceiver,msg->message);
                        fflush(stdout);

                        ret = STANDARD;
                }
                else
                if(msg->type==SEARCH)           //message dont on ne connait pas l'ip du destinataire
                {
                        if(isAlreadyReceived(msg)==FALSE)
                        {
                                addMsgReceived(*msg);
                                if(strcmp(msg->nameReceiver, adressBook.me.name)==0)
                                {
                                        printf("* %s vous a contacté et a été ajouté\n",msg->nameSender);
                                
                                        sleep(1);
                                
                                        printf(" ...\n# %s@%s : %s\n> ... ",msg->nameSender,msg->nameReceiver,msg->message);
                                        fflush(stdout);
                                
                                        addContact(msg->nameSender, msg->ipSender);
                        
                                        Message backsearch_msg;
                                
                                        /* Message type */
                                        backsearch_msg.type = BACKSEARCH;
                                        /* Sender IP */
                                        strcpy(backsearch_msg.ipSender, adressBook.me.ip);
                                        /* Sender name */
                                        strcpy(backsearch_msg.nameSender, adressBook.me.name);
                                        /* Message contents (none) */
                                        backsearch_msg.size = 0;
                                        backsearch_msg.message = NULL;
                                        // Contact name
                                        strcpy(backsearch_msg.nameReceiver, msg->nameSender);   
                                        // Contact ip
                                        strcpy(backsearch_msg.ipReceiver, msg->ipSender);
                                        
                                        send_message(backsearch_msg);
                                }
                                else
                                {

                                        int exist = searchContactIndex(msg->nameReceiver);

                                        if(exist!=-1)                           // the contact is in "adressBook"
                                        {
                                                strcpy(msg->ipReceiver, adressBook.contacts[exist].ip);
                                                send_message(*msg);
                                        }
                                        else
                                        {
                                                for(i=0;i<adressBook.nbContacts;i++)
                                                {
                                                        Contact c = adressBook.contacts[i];
                        
                                                        if(c.connected)
                                                        {               
                                                                // Contact ip
                                                                strcpy(msg->ipReceiver, c.ip);
                
                                                                send_message(*msg);
                                                        }
                                                }
                                        }
                                }
                        }
                        else                            // the message has been  already received
                        {
                                ret = ALREADY;
                        }
                }
        
        free(msg->message);
        return &ret;
}



